<?php
namespace app\manage\controller;
use my\MyRedis;
use my\smtp;
use think\Cache;

class Chat extends Base
{

    public function historyMessage()
    {
        $myId = $this->userinfo['admin_id'];
        $redis = MyRedis::getInstance(['select'=>1]);
        $count = $redis::lLen('message');
        $pageSize = 50;
        $res = $redis::lRange('message',$count-$pageSize,$count-1);
        $list = [];
        foreach ($res as $v) {
            $json = json_decode($v,true);
            if($json['message']['toUid']) {
                //私聊
                if($json['message']['toUid'] === $myId || $json['message']['fromId']) {
                    $list[] = $json;
                }
            }else {
                //群发
                $list[] = $json;
            }
        }
        return ajax($list);
//        $event = "chat message";
//        $message = date('Y-m-d H:i:s');
//        $array = [
//            'username' => '姜海蕤',
//            'age' => 30
//        ];
//        $res = $this->emit($event,$array);
//        halt($res);
    }


    private function emit($event ,$message) {
        $data = [$event,$message];
        $object = json_decode('{}');
        $array = [
            "emitter",
            [
                'type' => 2,
                'data' => $data,
                'nsp' => '/'
            ],
            [
                'rooms' => [],
                'flags' => $object,
                'except' => []
            ],
        ];
        $publish_data = msgpack_pack($array);
        $redis = MyRedis::getInstance();
        $res = $redis::publish('socket.io#/#',$publish_data);
        return $res;
    }







}